package com.xauto.pioneer.appuser.activity.mine.user;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.activity.mine.balance.BankListActivity;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.interfaces.AccountInfoCallBack;
import com.xauto.pioneer.appuser.model.AccountInfo;
import com.xauto.pioneer.appuser.model.Bank;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.model.Member;
import com.xauto.pioneer.appuser.util.DataCheckUtil;
import com.xauto.pioneer.appuser.util.DataLoadUtil;
import com.xauto.pioneer.appuser.util.StringUtils;
import com.xauto.pioneer.appuser.util.ViewUtil;
import com.xauto.pioneer.appuser.view.PromptDialog;
import com.xauto.pioneer.appuser.view.TopBar;

import net.tsz.afinal.FinalDb;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by lsx on 2018-01-16.
 */

public class BnakInformationActivity extends BaseActivity {
    private static  String BANK_AUTH_STATUS = "001";
    @Bind(R.id.user_topBar)
    TopBar user_topBar;
    /**银行信息界面*/
    /** 输入-银行卡号*/
    @Bind(R.id.user_bank_view)
    LinearLayout user_bank_view;
    /** 驾驶证审核中界面*/
    @Bind(R.id.user_layoutPromptStatus) LinearLayout layoutPromptStatus;
    @Bind(R.id.user_etDepositWithDrawBankNum)
    EditText user_etDepositWithDrawBankNum;
    /** 输入-银行卡持卡人姓名*/
    @Bind(R.id.user_etDepositWithDrawBankName) EditText user_etDepositWithDrawBankName;
    /** 输入-银行卡持卡人身份证号*/
    @Bind(R.id.user_etDepositWithDrawIdNumber) EditText user_etDepositWithDrawIdNumber;
    /** 申请提现*/
    @Bind(R.id.user_tvDepositWithDraw) TextView user_tvDepositWithDraw;
    @Bind(R.id.user_etAccountWithDrawBankType) TextView user_etAccountWithDrawBankType;
    private PromptDialog promptDialog;
    //会员账户信息
    AccountInfo accountInfo;
    //会员账户ID
    String member_account_id = "";
    //ID
    String bankId = "";
    //0-未认证 1-已认证 2-认证中
    private int BankStatus;
    @Override
    protected int getContentView() {
        return R.layout.layout_use_bank_info;
    }
    /**
     * 入口
     *
     * @param mContext
     */
    public static void startAction(Context mContext, int type) {
        Intent intent = new Intent(mContext, BnakInformationActivity.class);
        intent.putExtra(BANK_AUTH_STATUS, type);
        mContext.startActivity(intent);
    }
    @Override
    protected void initView() {
        user_topBar.showView(TopBar.LEFT_IMAGE_BTN, TopBar.TITLE);
        user_topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        user_topBar.setText(TopBar.TITLE, R.string.deposit_Bank);
        user_topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        user_etDepositWithDrawBankNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String bankName = user_etDepositWithDrawBankName.getText().toString().trim();
                if (!StringUtils.isBlank(bankName) && !StringUtils.isBlank(s.toString())){
                    ViewUtil.GetViewShgape(user_tvDepositWithDraw);
                    user_tvDepositWithDraw.setClickable(true);
                    user_tvDepositWithDraw.setBackgroundResource(R.drawable.bg_btn_green_selector);
                    ViewUtil.SetViewShgape(user_tvDepositWithDraw);
                }else {
                    ViewUtil.GetViewShgape(user_tvDepositWithDraw);
                    user_tvDepositWithDraw.setClickable(false);
                    user_tvDepositWithDraw.setBackgroundResource(R.drawable.bg_btn_gray);
                    ViewUtil.SetViewShgape(user_tvDepositWithDraw);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        user_etDepositWithDrawBankName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String bankNo = user_etDepositWithDrawBankNum.getText().toString().trim();
                if (!StringUtils.isBlank(bankNo) && !StringUtils.isBlank(s.toString())){
                    ViewUtil.GetViewShgape(user_tvDepositWithDraw);
                    user_tvDepositWithDraw.setClickable(true);
                    user_tvDepositWithDraw.setBackgroundResource(R.drawable.bg_btn_green_selector);
                    ViewUtil.SetViewShgape(user_tvDepositWithDraw);
                }else {
                    ViewUtil.GetViewShgape(user_tvDepositWithDraw);
                    user_tvDepositWithDraw.setClickable(false);
                    user_tvDepositWithDraw.setBackgroundResource(R.drawable.bg_btn_gray);
                    ViewUtil.SetViewShgape(user_tvDepositWithDraw);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        user_etDepositWithDrawIdNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String bankName = user_etDepositWithDrawIdNumber.getText().toString().trim();
                if (!StringUtils.isBlank(bankName) && !StringUtils.isBlank(s.toString())){
                    ViewUtil.GetViewShgape(user_tvDepositWithDraw);
                    user_tvDepositWithDraw.setClickable(true);
                    user_tvDepositWithDraw.setBackgroundResource(R.drawable.bg_btn_green_selector);
                    ViewUtil.SetViewShgape(user_tvDepositWithDraw);
                }else {
                    ViewUtil.GetViewShgape(user_tvDepositWithDraw);
                    user_tvDepositWithDraw.setClickable(false);
                    user_tvDepositWithDraw.setBackgroundResource(R.drawable.bg_btn_gray);
                    ViewUtil.SetViewShgape(user_tvDepositWithDraw);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        promptDialog = new PromptDialog(mContext, getString(R.string.notice), getString(R.string.text_not_network),
                getString(R.string.to_setting), getString(R.string.logout),getString(R.string.dialogpic_notice), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //去设置
                promptDialog.dismiss();
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //退出
                promptDialog.dismiss();
            }
        });
    }
    @OnClick({R.id.user_tvDepositWithDraw,R.id.user_etAccountWithDrawBankType})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.user_tvDepositWithDraw:
                String bankNo = user_etDepositWithDrawBankNum.getText().toString().trim();
                String bankName = user_etAccountWithDrawBankType.getText().toString().trim();
                String userName = user_etDepositWithDrawBankName.getText().toString().trim();
                String IdNumber = user_etDepositWithDrawIdNumber.getText().toString().trim();
                //申请验证
                TakeCar_submitTransaction(bankNo,bankName,userName,IdNumber);
                break;
            case R.id.user_etAccountWithDrawBankType:
                startActivityForResult(new Intent(mContext,BankListActivity.class),100);
                break;
        }
    }
    @Override
    protected void initData() {
        BankStatus = getIntent().getIntExtra(BANK_AUTH_STATUS,0);
        showData();
        //再次预加载
        DataLoadUtil.getMemberAccountInfo(mContext, new AccountInfoCallBack() {
            @Override
            public void getAccountInfo(AccountInfo info) {
                accountInfo = info;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showData();
                    }
                });
            }
        });
        showView();
    }
    /**
     * 显示相应视图
     */
    private void showView(){
        switch (BankStatus){
            case 0:
                user_bank_view.setVisibility(View.VISIBLE);
                layoutPromptStatus.setVisibility(View.GONE);
                break;
            case 1:
                user_bank_view.setVisibility(View.VISIBLE);
                layoutPromptStatus.setVisibility(View.GONE);
                //审核通过回显照片
                showData();
                //再次预加载
                DataLoadUtil.getMemberAccountInfo(mContext, new AccountInfoCallBack() {
                    @Override
                    public void getAccountInfo(AccountInfo info) {
                        accountInfo = info;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                showData();
                            }
                        });
                    }
                });
                user_tvDepositWithDraw.setVisibility(View.GONE);
                break;
            case 2:
                user_bank_view.setVisibility(View.GONE);
                layoutPromptStatus.setVisibility(View.VISIBLE);
                break;
        }
    }
    /**
     * 回显相关数据
     */
    private void showData(){
        if (accountInfo != null){
            member_account_id = accountInfo.member_account_id;
            user_etDepositWithDrawBankNum.setText(accountInfo.bank_account_number);
            user_etDepositWithDrawBankName.setText(accountInfo.bank_account_name);
            user_etDepositWithDrawIdNumber.setVisibility(View.GONE);
        FinalDb finalDb = FinalDb.create(mContext);
        List<Bank> bankList = finalDb.findAll(Bank.class);
        // 根据a-z进行排序源数据
        Collections.sort(bankList,new PinyinComparator());
        if (bankList != null && bankList.size() > 0){
            user_etAccountWithDrawBankType.setText(bankList.get(Integer.valueOf(accountInfo.bank_id).intValue()-1).getBank_name());
            //bankId = bankList.get(0).getBank_id();
            bankId= accountInfo.bank_id;
        } }
    }

    /**
     *提交银行卡信息
     */
    private void TakeCar_submitTransaction(String bankNo,String bankName,String userName,String IdNumber){
        Member member = DataCheckUtil.getMemberObj(mContext);
        new LHttpLib().TakeCar_submitTransaction(mContext,userName,memberId,bankId,
                bankName,bankNo,IdNumber,new BaseLHttpHandler(mContext,true,true){
                    @Override
                    public void onSuccess(JSONStatus jsonStatus) {
                        super.onSuccess(jsonStatus);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                prompt("银行卡认证通过");
                                finish();

                            }
                        });
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 100 && resultCode == RESULT_OK){
            user_etAccountWithDrawBankType.setText(data.getStringExtra("bank_name"));
            bankId = data.getStringExtra("bank_id");
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    public class PinyinComparator implements Comparator<Bank> {

        public int compare(Bank o1, Bank o2) {
            if (o1.getBank_prefix().equals("@")
                    || o2.getBank_prefix().equals("#")) {
                return -1;
            } else if (o1.getBank_prefix().equals("#")
                    || o2.getBank_prefix().equals("@")) {
                return 1;
            } else {
                return o1.getBank_prefix().compareTo(o2.getBank_prefix());
            }
        }

    }
}
